import { _decorator, Component, Node, EditBox } from 'cc';
import { ParamUI } from './ParamUI';
import UiManager from './UiManager';
import { plog } from '../Utils/Tools';
const { ccclass, property } = _decorator;
import NetEventMgr from '../Game/NetEventMgr';
import msg from '../Msg/msgpb.js';
import { DataMgr } from '../Game/DataMgr';
// import { msg } from '../Msg/proto.js';

@ccclass('UiRegister')
export class UiRegister extends Component {
    @property(Node)
    nodeBack: Node = null;
    @property(Node)
    btnNodeRegister: Node = null;
    @property(Node)
    btnNodeLogin: Node = null;
    @property(EditBox)
    ebAccount: EditBox = null;
    @property(EditBox)
    ebPassword1: EditBox = null;
    @property(EditBox)
    ebPassword2: EditBox = null;



    onLoad(): void {
        this.btnNodeLogin.on("click", this.onBtnLoginClick, this);
        this.btnNodeRegister.on("click", this.onBtnRegisterClick, this);
        this.node.on(Node.EventType.TOUCH_END, this.onOutClick, this);
        this.nodeBack.on(Node.EventType.TOUCH_END, this.onBackClick, this);
    }
    onDestroy() {
        this.btnNodeLogin.off("click", this.onBtnLoginClick, this);
        this.btnNodeRegister.off("click", this.onBtnRegisterClick, this);
        this.node.off(Node.EventType.TOUCH_END, this.onOutClick, this);
        this.nodeBack.off(Node.EventType.TOUCH_END, this.onBackClick, this);
    }
    onBackClick(evt) {
        evt.bubbles = false
    }
    onOutClick(evt) {
        evt.bubbles = false
        UiManager.Instance.RemoveUI(this.node, this.node.name)
    }
    onBtnLoginClick() {
        // let pu = this.node.getComponent(ParamUI)
        // pu.MsgBox("切换到登录？", (ok) => {
        //     if (ok) {
        //         UiManager.Instance.ChangeUI(this.node, "from reg", 'login')
        //     }
        // })
        UiManager.Instance.ChangeUI(this.node, "from reg", 'login')
    }

    onBtnRegisterClick() {
        let pu = this.getComponent(ParamUI)
        if (this.ebAccount.string == "") {
            pu.Hint("账号不能是空")
            return
        }
        if (this.ebPassword1.string == "") {
            pu.Hint("密码不能是空")
            return
        }
        if (this.ebPassword1.string !== this.ebPassword2.string) {
            pu.Hint("两次密码不一样")
            return
        }

        /**
        let nem = NetEventMgr.Instance
        nem.StartConnect(() => {
            const mr = msg.ClientEnterR
            const ma = msg.ClientEnterA
            let msgR = mr.create()
            msgR.Accout = this.ebAccount.string
            msgR.Password = this.ebPassword1.string
            msgR.isLogin = false
            nem.SendMessage(mr.name, ma.name, mr.encode(msgR).finish(), (data) => {
                let pu = this.getComponent(ParamUI)
                let a = ma.decode(data);
                if (a.Err != null) {
                    pu.Hint(a.Err.Result)
                } else {
                    pu.Hint("注册成功", () => {
                        UiManager.Instance.ChangeUI(this.node, "from login", 'login')
                        DataMgr.Instance.SetAccountInfo(this.ebAccount.string, this.ebPassword1.string)
                    })
                }
            })
        })
        /**/
    }


    start() {

    }

    update(deltaTime: number) {

    }

}

